#include <ma.h>
#include <mavsprintf.h>
#include <MAUtil/Moblet.h>
#include <NativeUI/Widgets.h>
#include <NativeUI/WidgetUtil.h>
#include <MAUtil/Graphics.h>
#include "MAHeaders.h"
#include "player.h"
#include "background.h"
#include "controls.h"
#include "objects.h"
#include "ObjectController.h"
#include "sound.h"
#include "map.h"


using namespace MAUtil;
using namespace NativeUI;



/**
 * Moblet to be used as a template for a Native UI application.
 */
class NativeUIMoblet : public Moblet, public ButtonListener,  public MAUtil::TimerListener, public FocusListener
{
public:
	/**
	 * The constructor creates the user interface.
	 */

	NativeUIMoblet()
	{
		//createUI();
		maScreenSetOrientation(SCREEN_ORIENTATION_LANDSCAPE);
		MAExtent screenSize = maGetScrSize();
		mScreenWidth = EXTENT_Y(screenSize);
		mScreenHeight = EXTENT_X(screenSize);
		offset = 50;
		game == false;

		//JumpButton->addButtonListener(this);

		camera = new Camera();
		//orb = new Orb((mScreenWidth/10)*3, (mScreenHeight/10)*8, 0);
		background = new Background();
		flatman = new Player(camera);
		controller = new Controls(mScreenWidth, mScreenHeight);
		objects = new ObjectController(camera);
		sound = new Sound();

		//createUI();
		//drawControls();
		//flatman->draw();
		//background->draw();
		//controller->draw();
		//background->drawTitle();
		sound->PlayIntro();
		addTimer(this, 30, -1);

	}

	/**
	 * Destructor.
	 */
	virtual ~NativeUIMoblet()
	{
		mButton->removeButtonListener(this);
		//JumpButton->removeButtonListener(this);
		// All the children will be deleted.
		delete mScreen;
		sound->StopAll();
	}

	void focusLost()
	{
		removeTimer(this);
	}

	void focusGained()
	{
		addTimer(this, 30, -1);
	}

	/**
	 * Create the user interface.
	 */
	void createUI()
	{
		/*// Create a NativeUI screen that will hold layout and widgets.
		mScreen = new Screen();

		//Create a Vertical Layout that will hold widgets
		mLayout = new VerticalLayout();

		/*JumpButton = new Button();

				//Let the button fill it's layout
		JumpButton->setWidth(100);
		JumpButton->fillSpaceVertically();

				//Set the moblet to receive events from the button
		JumpButton->addButtonListener(this);

				//Set the text of the button
		JumpButton->setText("JUMP!");

				//Add the button to the layout
		mLayout->addChild(JumpButton);
		//Let the layout fill the entire screen*/

		//mLayout->fillSpaceHorizontally();
		//mLayout->fillSpaceVertically();

/*
		//Create a Native UI button
		mButton = new Button();



		//Set the moblet to receive events from the button
		mButton->addButtonListener(this);

		//Set the text of the button
		mButton->setText("Play Game");

		//Add the button to the layout
		mLayout->addChild(mButton);

		//Create a Native UI button


		//Add the layout to the screen
		//mScreen->setMainWidget(mLayout);
*/

		//Show the screen
		//mScreen->show();*/
	}



	/**
	 * Called when a key is pressed.
	 */
	void keyPressEvent(int keyCode, int nativeCode)
	{
		if (MAK_UP == keyCode || MAK_MENU == keyCode)
		{
			if (flatman->GetJumping() == false)
			{
				flatman->SetSpd_y(14);
				flatman->IsJumping();
			}
		}
		else if (MAK_BACK == keyCode || MAK_0 == keyCode || MAK_HOME == keyCode)
		{

			sound->StopAll();
			close();
		}
	}

	void pointerPressEvent(MAPoint2d point)
	{
		//if (point.y > mScreenHeight-offset)
		//{
		//	if (point.x < controlDisplayWidth)
		//		keyPressEvent(MAK_1, 0);
			//else if (point.x < controlDisplayWidth * 2)
		//		keyPressEvent(MAK_DOWN, 0);
		//}
		if (point.x > 0 && game == false)
		{
				game = true;
				sound->StopIntro();
				sound->PlayLevel1();

		}

		if (point.x > mScreenWidth - (mScreenWidth/5)){
			if(flatman->GetJumping() == false)
			{
				flatman->SetSpd_y(14);
				flatman->IsJumping();
				sound->PlayJump();

			}
		}
		else if (point.x < (mScreenWidth/5))
			if(flatman->GetArmor() == 0)
				flatman->SetArmor(1);
			else
				flatman->SetArmor(0);
	}

	/**
	* This method is called if the touch-up event was inside the
	* bounds of the button.
	* @param button The button object that generated the event.
	*/
	virtual void buttonClicked(Widget* button)
	{
		/*if (button == JumpButton && flatman->IsOnGround() == true)
			flatman->IsJumping();*/
		//((Button*) button)->setText("This is the game");



	}


	void runTimerEvent()
	{
		int i_cam = flatman->GetLoc_x();
		objects->p_collide(flatman);
		//objects->o_collide(flatman);

		if(game == true)
		{
			//collision();
			background->draw();
			//orb->draw();
			objects->draw();
			flatman->draw();
			//drawControls();
		}
		else
		{
			background->drawTitle();
		}
		Gfx_updateScreen();
		camera->camIncrement(flatman->GetLoc_x()- i_cam);

	}

	void collision(){
			if(flatman->GetLoc_x() == orb->getx() && flatman->GetArmor() != orb->getColor())
				flatman->SetSpd_x(0);


		}
/*
	void collision(){
		if(flatman->GetLoc_x() > 1)
		{
			flatman->iscollision();
			sound->PlayCollision();
		}

	}
*/
private:
    Screen* mScreen;			//A Native UI screen
    VerticalLayout* mLayout;	//A Native UI layout
    Button* mButton;			//A Native UI button
    //Button* JumpButton;         //A Native UI jumping button
    Orb* orb;
    Player* flatman;
    Background* background;
    Controls* controller;
    ObjectController* objects;
    Camera* camera;
    Sound* sound;
    int mScreenWidth, mScreenHeight;
    int offset;
    int controlDisplayWidth;
    bool game;
    MAAudioData mData[3];
     // The THREE audio instance handles
    MAAudioInstance mInstance[3];

};

/**
 * Main function that is called when the program starts.
 */
extern "C" int MAMain()
{
	Moblet::run(new NativeUIMoblet());
	return 0;
}
